//
//  EPMPhotoView.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/29.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMPhotoView.h"
#import "EPMPhotoCell.h"

@interface EPMPhotoView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong,nonatomic) UICollectionViewFlowLayout* layout;
@end
static NSString* Cell_ID = @"cell_ID";
static CGFloat sizeMargin = 5;


@implementation EPMPhotoView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    CGFloat itemWH = (screenWidth - 2 * ViewMargine - 2 * sizeMargin)/3;
    UICollectionViewFlowLayout* flowLayout = [UICollectionViewFlowLayout new];
    self.layout = flowLayout;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.itemSize = CGSizeMake(itemWH, itemWH);
    if (self = [super initWithFrame:frame collectionViewLayout:flowLayout]){
        [self setupUI];
    }
    return self;
    
}

- (void)setupUI{
    self.backgroundColor = [UIColor whiteColor];
    self.dataSource = self;
    self.delegate = self;
    self.bounces = false;
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    [self registerClass:[EPMPhotoCell class] forCellWithReuseIdentifier:Cell_ID];
}
- (CGSize)dealPhotoViewSise:(unsigned long)count{
    
    if ( count == 1){
        UIImage* image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:_modelList.lastObject.thumbnail_pic];
        CGFloat W = image.size.width < 80 ? 80 : image.size.width;
        CGFloat H = image.size.height > 150 ? 150 :  image.size.height;
        return CGSizeMake(W, H);
    }
    
    int row =  count == 4 ? 2 : ((int)count-1)/3 + 1;
    int col = count == 4 ? 2 : (count >= 3 ? 3 : count%3);
    CGFloat itemWH = (screenWidth - 2 * ViewMargine - 2 * sizeMargin)/3;
    CGFloat W = col * itemWH + (col - 1)*sizeMargin;
    CGFloat H = row * itemWH + (row - 1)*sizeMargin;
    return  CGSizeMake(W, H);
}



#pragma
#pragma-数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.modelList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EPMPhotoCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cell_ID forIndexPath:indexPath];
    cell.imgName = _modelList[indexPath.item].thumbnail_pic;
    return  cell;
}

- (void)setModelList:(NSArray<EPMHomePictureUrlModel *> *)modelList{
    _modelList = modelList;
    CGSize size = [self dealPhotoViewSise:_modelList.count];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
    }];
    if (modelList.count == 1){
        self.layout.itemSize = size;
    }else{
        CGFloat itemWH = (screenWidth - 2 * ViewMargine - 2 * sizeMargin)/3;
        self.layout.itemSize = CGSizeMake(itemWH, itemWH);
    }
    [self layoutIfNeeded];
}



@end
